package com.kavin.collection.janino;

import org.codehaus.commons.compiler.IScriptEvaluator;
import org.codehaus.janino.ExpressionEvaluator;
import org.codehaus.janino.ScriptEvaluator;

/**
 * Janino 执行java代码
 */
public class JaninoTest {

    public static void main(String[] args) {
        //printContent();
        //mathExpress();
        runJavaCode();
        //execNestMethod();
    }


    /**
     * 简单打印
     */
    public static void printContent(){
        try {
            String content="System.out.println(\"Hello world\");";
            IScriptEvaluator evaluator = new ScriptEvaluator();
            evaluator.cook(content);
            evaluator.evaluate(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void mathExpress(){
        try {
            String express = "(1+2)*3";
            ScriptEvaluator evaluator = new ExpressionEvaluator();
            evaluator.cook(express);
            Object res = evaluator.evaluate(null);
            System.out.println(express + "=" + res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 方法嵌套调用
     */
    public static void execNestMethod() {
        try {
            ScriptEvaluator se = new ScriptEvaluator();
            se.setParameters(new String[] { "arg1", "arg2" }, new Class[] { String.class, int.class });
            se.cook(
                    ""
                            + "System.out.println(arg1);\n"
                            + "System.out.println(arg2);\n"
                            + "\n"
                            + "static void method1() {\n" +
                            "       method2();\n"
                            + "    System.out.println(\"run in method1()\");\n"
                            + "}\n"
                            + "\n"
                            + "public static void method2() {\n"
                            + "    System.out.println(\"run in method2()\");\n"
                            + "}\n"
                            + "\n"
                            + "method1();\n"
                            + "\n"

            );
            se.evaluate(new Object[]{"aaa",22});
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 手写java代码，引入第三方工具。
     */
    public static void runJavaCode(){
        try{
            String javaCode =
                    "import java.util.*;" +
                    "import com.alibaba.fastjson.*;" +
                            "method();" +
                            "public static void method(){" +
                                "Map map =new HashMap();" +
                                "map.put(\"key\",\"1234\");" +
                                "String a = JSON.toJSONString(map);" +
                                "System.out.println(a);" +
                            "}";
            IScriptEvaluator evaluator = new ScriptEvaluator();
            evaluator.cook(javaCode);
            evaluator.evaluate(null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
